{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5e348d64",
   "metadata": {},
   "source": [
    "# P67练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "3caf5858",
   "metadata": {},
   "outputs": [],
   "source": [
    "phrase = \"Don't panic!\" # 这是一个字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "38625e21",
   "metadata": {},
   "outputs": [],
   "source": [
    "plist = list(phrase) # 列出字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a14ddc3b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n"
     ]
    }
   ],
   "source": [
    "print(phrase) # 输出字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "f0351c0b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n"
     ]
    }
   ],
   "source": [
    "print(plist) # 输出每一个字符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "629df169",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(4): # pop是默认删除最后一位，range(4)是循环四次，即删除最后四位，剩下“Don't pa”\n",
    "    plist.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "08dec143",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plist.pop(0) # 删除第一位“D”,剩下“on't pa”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "08e7231f",
   "metadata": {},
   "outputs": [],
   "source": [
    "plist.remove(\"'\") # remove移除“’”，剩下“ont pa”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "09a946c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "plist.extend([plist.pop(),plist.pop()]) # 1.先看括号里面，用pop将列表末尾两个对象弹出来；2.然后看外面的extend，将弹出的对象增加到plist末尾\n",
    "# 1.括号里面\"a,p\"；2.括号外面extend（a,p）到列表末尾；最后结果即“ont ap”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "e9619d9d",
   "metadata": {},
   "outputs": [],
   "source": [
    "plist.insert(2,plist.pop(3)) # plist.pop(3)取出空格，然后将空格insert插入到索引位置2之前，即\"t\"之前，现在是“on tap”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "b28014b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "new_phrase = ''.join(plist) # 把“plist”转换回一个字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "ba308e9e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['o', 'n', ' ', 't', 'a', 'p']\n"
     ]
    }
   ],
   "source": [
    "print(plist) # 输出列表元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "9d4d80e8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "on tap\n"
     ]
    }
   ],
   "source": [
    "print(new_phrase) # 输出新字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7fa32b85",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
